# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <unistd.h>
# include "../include/CreateVFS.h"
# include "../include/Header.h"
# include "../include/NArrayTree.h"
# include "../include/MountVFS.h"
# include "../include/Hashing.h"
# include "../include/BinarySearchTree.h"
# include "../include/DirectoryOps.h"
int main () {
	int choice,flag = 0;
	char fileName[100];
	char OutputFile[100];
	char *DirPath, *DirName;
	DirPath = (char*)malloc(sizeof(char*)*80);
	DirName = (char*)malloc(sizeof(char*)*20);
	struct VFS *mainHeader;
	struct NArray_node *NArrayRoot = NULL;
	struct BST_node *BSTRoot = NULL;
	struct HashNode **table;
	do {
		//fork();
		//execve("/usr/bin/clear", NULL, NULL);
		printf ("\t\tMy Virtual File System\n\n");
		printf ("1. Create VFS from File\n");
		printf ("2. Mount VFS\n");
		printf ("3. UnMount VFS\n");
		printf ("4. Display NArray Tree\n");
		printf ("5. Display Binary Search Tree\n");
		printf ("6. Display Hash Table\n");
		printf ("7. Make Directory\n");
		printf ("8. List Directory\n");
		printf ("9. Delete Directory\n");
		printf ("10. Exit \n");
		printf ("\n\nEnter your choice : ");
		scanf ("%d", &choice);
		switch (choice) {
			case 1:
				printf("Enter filename to create: ");
				scanf("%s", fileName);
				mainHeader = create_block(fileName);
				read_block(mainHeader, fileName);
				NArrayRoot = mountNArray (mainHeader, NArrayRoot);
				BSTRoot = mountBST (mainHeader, BSTRoot);
				table = mountHashTable (mainHeader, table);
				displayNArray(NArrayRoot);
				displayBST(BSTRoot);
				break;
			case 2:
				printf("Enter VFS filename to mount: ");
				scanf("%s", fileName);
				mainHeader = create_block(fileName);
				mount_vfs(mainHeader, fileName);
				NArrayRoot = mountNArray (mainHeader, NArrayRoot);
				BSTRoot = mountBST (mainHeader, BSTRoot);
				table = mountHashTable (mainHeader, table);
				break;
			case 3:
				unmount_vfs(mainHeader);
				freeNArrayNode (NArrayRoot);
				freeBSTNode (BSTRoot);
				free (table);
				NArrayRoot = NULL;
				BSTRoot = NULL;
				table = NULL;
				break;
			case 4:
				if (NArrayRoot)
					displayNArray(NArrayRoot);
				else
					printf ("No items to display NArray Tree\n");
				break;
			case 5:
				if (BSTRoot)
					displayBST(BSTRoot);
				else
					printf ("No items to display Binary Search Tree\n");
				break;
			case 6:
				if (table)
					displayHashTable(table);
				else
					printf ("No items to display Hash Table\n");
				break;
			case 7:
				printf("Enter the Directory path : ");
				scanf("%s", DirPath);
				printf("Enter the Directory name to create : ");
				scanf("%s", DirName);
				makeDir(DirPath, DirName, NArrayRoot, BSTRoot);
				printf("Directory Created Successfully\n");
				break;	
			case 8:
				printf("Enter the Directory name : ");
				scanf("%s", DirName);
				printf("Enter the Flag :");
				scanf("%d", &flag);
				printf("Enter the OutputFile name :");
				listDir(DirName, flag, OutputFile, NArrayRoot, BSTRoot);
				break;						
			case 9:
				printf("Enter the Directory path :");
				scanf("%s", DirPath);
				printf("Directory path:%s\n", DirPath);
				if(deleteDir(DirPath,mainHeader)) {
					freeNArrayNode (NArrayRoot);
					freeBSTNode (BSTRoot);
					free (table);
					NArrayRoot = NULL;
					BSTRoot = NULL;
					table = NULL;
					NArrayRoot = mountNArray (mainHeader, NArrayRoot);
					BSTRoot = mountBST (mainHeader, BSTRoot);
					table = mountHashTable (mainHeader, table);					
					printf("Directory structure updated\n");				
				}
				else
					printf("Directory not found\n");
				break;									
			case 10:
				exit(0);
		
		}
	}while(1);
	return 0;
}
